<div class="full_w">
    <div class="h_title"><?php echo Yii::t('AdmincpModule.admincp', 'Add Article'); ?></div>
    <?php 
        foreach(Yii::app()->admin->getFlashes() as $key => $message){
            echo '<div class="n_'.$key.'"><p>'.$message."</p></div>\n";
        }
    ?>
    <form action="" method="post" id="frm">
        <div class="element">
            <label for="name">Title <span class="red">*</span></label>
            <input type="text" name="post[post_title]" id="title" class="text required" value="<?php echo isset($arrItemInfo['post_title'])?$arrItemInfo['post_title']:null; ?>">
        </div>
        <div class="element">
            <label for="name">Intro text:</label>
            <textarea name="post[post_intro]" class="textarea" id="intro"><?php echo isset($arrItemInfo['post_intro'])?$arrItemInfo['post_intro']:null; ?></textarea>
        </div>
        <div class="element">
            <label for="name">Body text:</label>
            <textarea name="post[post_body]" class="textarea" id="body"><?php echo isset($arrItemInfo['post_body'])?$arrItemInfo['post_body']:null; ?></textarea>
        </div>
        <?php if(!empty($arrCategories)): ?>
        <div class="element">
            <label for="category">Category <span class="red">(required)</span></label>
            <div id="category-list" style=" max-height: 200px; min-height: 42px; overflow: auto;">
                <ul>
                <?php foreach($arrCategories as $R): ?>
                	<li>
						<?php for($i=0; $i<$R['category_level']; $i++): echo '&nbsp;&nbsp;&nbsp;'; endfor; ?>
                        <input type="checkbox" name="category[]" value="<?php echo $R['category_id']; ?>" <?php if(isset($arrItemInfo['post_category_id']) && in_array($R['category_id'], $arrItemInfo['post_category_id'])):?>checked<?php endif; ?>> 
                        <?php echo $R['category_name'].'<br>'; ?>
                 	</li>
				 <?php endforeach; ?>
                 </ul>
             </div>
            <!--
            <select name="category[]" id="parent">
                <option value="">-- select category</option>
                <?php foreach($arrCategories as $R): ?>
                <option value="<?php echo $R['category_id']; ?>" <?php if(isset($arrItemInfo['category_parent']) && $R['category_id'] == $arrItemInfo['category_parent']): ?>selected<?php endif; ?>>
                    <?php for($i=0; $i<$R['category_level']; $i++): echo '&nbsp;&nbsp;&nbsp;'; endfor; ?>
					<?php echo $R['category_name']; ?>
                </option>
                <?php endforeach; ?>
            </select>
            -->
        </div>
        <?php endif; ?>
        <div class="element">
            <label for="name">Images</label>
            <input type="text" name="post[post_image]" id="image" class="text" value="<?php echo isset($arrItemInfo['post_image'])?$arrItemInfo['post_image']:null; ?>"> 
            [<a class="fancybox" onclick="Media.show({callback: function(src){$('#image').val(src); $.fancybox.close();}, label: 'post'});" href="javascript:void(0);">Media</a>] 
        </div>
        <div class="element">
            <label for="name">Tag</label>
            <input type="text" name="tags" id="tags" class="text" value="<?php echo isset($arrItemInfo['post_tags'])?$arrItemInfo['post_tags']:null; ?>">
        	<p class="description">Separate tags with commas</p>
        </div>
        <div class="element">
            <input type="checkbox" name="post[post_allow_comment]" id="allow_comment" <?php if(isset($arrItemInfo['post_allow_comment']) && $arrItemInfo['post_allow_comment'] == 1):?>checked<?php endif;?> value="1" style="float:left; margin-left:5px;">
            <label for="allow_comment"  style="float:left;">Allow comments</label>
        </div>
        <div class="element">
            <input type="checkbox" name="post[post_allow_search]" id="allow_search" <?php if(isset($arrItemInfo['post_allow_search']) && $arrItemInfo['post_allow_search'] == 1):?>checked<?php endif;?> value="1" style="float:left; margin-left:5px;">
            <label for="allow_search"  style="float:left;">Searchable</label>
        </div>
        <div class="element">
            <label for="name">Published date</label>
            <input type="text" name="post[post_published_date]" id="published" class="text" value="<?php echo isset($arrItemInfo['post_published_date'])?$arrItemInfo['post_published_date']:null; ?>">
        	<p class="description">(dd-mm-yyyy) GMT+7 | Leave it blank means the post is published now</p>
        </div>
        <div class="element">
            <label for="name">Expiry date</label>
            <input type="text" name="post[post_expiry_date]" id="expiry" class="text" value="<?php echo isset($arrItemInfo['post_expiry_date'])?$arrItemInfo['post_expiry_date']:null; ?>">
        	<p class="description">(dd-mm-yyyy) GMT+7 | Leave it blank means the post is never expired</p>
        </div>
        
        <?php foreach($arrCustomFields as $R): ?>
        <div class="element">
            <label for="<?php echo $R['field_name']; ?>"><?php echo $R['field_name']; ?></label>
            <?php if ($R['field_input_type'] == 'text'): ?>
            <input type="text" name="fields[<?php echo $R['field_id']; ?>]" id="fields_<?php echo $R['field_id'];?>" class="text" value="<?php echo isset($arrItemInfo['fields'][$R['field_name']])?$arrItemInfo['fields'][$R['field_name']]:null; ?>">
            <?php elseif ($R['field_input_type'] == 'file'): ?>
            <input type="text" name="fields[<?php echo $R['field_id']; ?>]" id="fields_<?php echo $R['field_id'];?>" class="text" value="<?php echo isset($arrItemInfo['fields'][$R['field_name']])?$arrItemInfo['fields'][$R['field_name']]:null; ?>">
			[<a class="fancybox" onclick="Media.show({callback: function(src){$('#fields_<?php echo $R['field_id'];?>').val(src); $.fancybox.close();}, label: 'post_field'});" href="javascript:void(0);">Media</a>] 
			<?php elseif ($R['field_input_type'] == 'editor'): ?>
			<textarea name="fields[<?php echo $R['field_id']; ?>]" id="fields_<?php echo $R['field_id'];?>" class="textarea ckeditor"><?php echo isset($arrItemInfo['fields'][$R['field_name']])?$arrItemInfo['fields'][$R['field_name']]:null; ?></textarea>
			<script type="text/javascript">
			$(function(){
				CKEDITOR.replace( 'fields_<?php echo $R['field_id'];?>',
				{
					toolbar:[
								[ 'Bold', 'Italic', 'Underline', '-', 'Cut', 'Copy', 'Paste', '-', 'Undo', 'Redo' ]
							]
				});
			});
			</script>
            <?php elseif ($R['field_input_type'] == 'textarea'): ?>
			<textarea name="fields[<?php echo $R['field_id']; ?>]" id="fields_<?php echo $R['field_id'];?>" class="textarea"><?php echo isset($arrItemInfo['fields'][$R['field_name']])?$arrItemInfo['fields'][$R['field_name']]:null; ?></textarea>
			<?php endif; ?>
            <p class="description"><?php echo $R['field_description']; ?></p>
        </div>
        <?php endforeach; ?>
        
        <div class="entry">
        	<input type="hidden" name="post[taxonomy_id]" value="2">
            <input type="hidden" name="id" id="id" value="<?php echo isset($arrItemInfo['post_id'])?$arrItemInfo['post_id']:null; ?>">
            <button type="submit">Preview</button> <button type="submit" class="add">Save post</button> <button class="cancel">Cancel</button>
        </div>
    </form>
</div>
<script type="text/javascript" src="js/media-popup.js"></script>
<script src="../libs/ckeditor/ckeditor.js"></script>
<script>
$(function(){
	$("#published").datepicker({
		dateFormat:'dd-mm-yy',
		minDate: '0',
		changeMonth: true,
		changeYear: true,
		onClose: function( selectedDate ) {
			if(selectedDate != ''){
				$("#expiry").datepicker( "option", "minDate", selectedDate );
			}
		}
	});
	$("#expiry").datepicker({
		dateFormat:'dd-mm-yy',
		timeFormat: 'hh:mm',
		minDate: '0',
		changeMonth: true,
		changeYear: true,
		onClose: function( selectedDate ) {
			if(selectedDate != ''){
				$("#published").datepicker( "option", "maxDate", selectedDate );
			}
		}
	});
	CKEDITOR.replace( 'intro', {
		toolbar:[
					[ 'Bold', 'Italic', 'Underline', '-', 'Cut', 'Copy', 'Paste', '-', 'Undo', 'Redo' ]
				]
	});
	CKEDITOR.replace( 'body', {
		toolbar: [
					{ name: 'document', items: [ 'Source', '-', 'NewPage', 'Preview', '-', 'Templates' ] },
					{ name: 'clipboard', items: [ 'Cut', 'Copy', 'Paste', 'PasteText', 'PasteFromWord', '-', 'Undo', 'Redo' ] },
					'/',
					{ name: 'basicstyles', items: [ 'Bold', 'Italic' ] }
				]
	});
	$('#frm').validate();
});
</script>